package com.itheima.wechat;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.OrderService;
import com.itheima.contant.MessageConstant;
import com.itheima.contant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/*用于体检预约的controller*/
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private JedisPool jedisPool;
    @Reference
    private OrderService orderService;

    @RequestMapping("/submit")
    public Result submit(@RequestBody Map map) {

        //TODO : 前台传回的结果的格式    身份证号 ：idCard
        //      {setmealId:12, idCard:'412824199510301414', sex:'1', name:'吴云龙', validateCode:'949336', telephone:'15044290589', orderDate:'2019-09-28'}
        //
        //                        setmealId:id,
        //                        name:'吴云龙',
        //                        telephone:'15044290589',
        //                        validateCode:'949336',
        //                        idCard:'412824199510301414',
        //                        orderDate:'2019-09-28',
        //                        sex:'1'
        //1、验证码得输对

        /*获取用户输入的验证码*/
        String code = (String) map.get("validateCode");
        /*获取用户输入的电话号码*/
        String telephone = (String) map.get("telephone");
        /*取出数据库中的验证码*/
        String redisCode = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        /*如果没有向此电话号码发送验证码或用户输入验证码错误
         *   返回验证码输入错误 */
        if (redisCode == null || redisCode.equals(code)== false) {
            return Result.error(MessageConstant.VALIDATECODE_ERROR);
        }

        Result result = null;

        try {
            /*预约类型是微信预约，将此预约类型传到后台去*/
            map.put("orderType",Order.ORDERTYPE_WEIXIN);

            //2、验证码输入正确，将预约信息传到后台进行存储
            result = orderService.memoryOrderInfo(map);
        } catch (Exception e) {
            return result;
        }

        if (result != null){
            //3、发送短信 TODO : 先用已经申请好的模版
            try {
                SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, (String) map.get("orderDate"));
            } catch (ClientException e) {
                return result;
            }
        }


        return result;
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){

        /*前端将会员id传回来
        返回给前端的数据格式
            {data.member}
            {data.setmeal}
            {data.orderDate}
            {data.orderType}
        * */
        try {
            Map<String,Object> map = orderService.queryOrderInfo(id);
            return Result.error("",map);
        } catch (Exception e) {
            return Result.error("");
        }
    }
}
